<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import ManagementView from '@/components/ManagementView.vue'
import AnalysisView from '@/components/AnalysisView.vue'
import { useUserStore } from '@/stores/modules/user'
import {
  Fold,
  Expand,
  Location,
  Aim,
  Search,
  Van,
  Star,
  Setting,
  DataAnalysis,
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 侧边栏折叠状态
const isCollapse = ref(false)
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

// 获取用户角色
const userRole = computed(() => userStore.userRole)

// 当前选中的菜单项
const activeMenu = ref('')

// 根据用户角色和路由设置默认菜单项
onMounted(() => {
  setActiveMenuFromRoute()
})

// 监听路由变化，更新活动菜单
watch(() => route.path, () => {
  setActiveMenuFromRoute()
})

// 根据路由路径设置活动菜单
function setActiveMenuFromRoute() {
  const path = route.path
  if (path.includes('/map-browsing')) {
    activeMenu.value = 'map-browsing'
  } else if (path.includes('/geographical-query')) {
    activeMenu.value = 'geographical-query'
  } else if (path.includes('/facility-search')) {
    activeMenu.value = 'facility-search'
  } else if (path.includes('/traffic-info')) {
    activeMenu.value = 'traffic-info'
  } else if (path.includes('/scenic-recommendation')) {
    activeMenu.value = 'scenic-recommendation'
  } else if (path.includes('/admin/analysis')) {
    activeMenu.value = 'analysis'
  } else if (path.includes('/admin/management')) {
    activeMenu.value = 'management'
  }
}

// 处理菜单点击事件
const handleMenuSelect = (index: string) => {
  if (userRole.value === 'admin') {
    if (index === 'analysis') {
      router.push('/admin/analysis')
    } else if (index === 'management') {
      router.push('/admin/management')
    }
  } else if (userRole.value === 'visitor') {
    // 游客角色导航到对应路由
    router.push(`/visitor/${index}`)
  }
}

// 退出登录
const handleLogout = () => {
  userStore.logout()
  router.push('/login')
}
</script>

<template>
  <el-container class="layout-container">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="logo-container">
        <el-icon class="toggle-icon" @click="toggleSidebar">
          <Fold v-if="!isCollapse" />
          <Expand v-else />
        </el-icon>
        <h2 class="logo-text">美丽南山胜境103旅游公路-ForGIS</h2>
      </div>
      <div class="header-right">
        <el-button type="danger" @click="handleLogout">退出登录</el-button>
      </div>
    </el-header>

    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="auto">
        <el-menu
          :collapse="isCollapse"
          class="sidebar-menu"
          background-color="#001529"
          text-color="#fff"
          active-text-color="#409EFF"
          :default-active="activeMenu"
          @select="handleMenuSelect"
          router
        >
          <!-- 游客可见的菜单项 -->
          <template v-if="userRole === 'visitor'">
            <el-menu-item index="map-browsing">
              <el-icon><Location /></el-icon>
              <template #title>地图浏览</template>
            </el-menu-item>
            <el-menu-item index="geographical-query">
              <el-icon><Aim /></el-icon>
              <template #title>地理范围查询</template>
            </el-menu-item>
            <el-menu-item index="facility-search">
              <el-icon><Search /></el-icon>
              <template #title>设施搜索</template>
            </el-menu-item>
            <!-- <el-menu-item index="traffic-info">
              <el-icon><Van /></el-icon>
              <template #title>交通信息</template>
            </el-menu-item> -->
            <el-menu-item index="scenic-recommendation">
              <el-icon><Star /></el-icon>
              <template #title>景点推荐</template>
            </el-menu-item>
          </template>

          <!-- 管理员可见的菜单项 -->
          <el-menu-item index="management" v-if="userRole === 'admin'">
            <el-icon><Setting /></el-icon>
            <template #title>综合管理</template>
          </el-menu-item>
          <el-menu-item index="analysis" v-if="userRole === 'admin'">
            <el-icon><DataAnalysis /></el-icon>
            <template #title>统计分析</template>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主要内容区域 -->
      <el-main class="main-content">
        <router-view v-if="userRole === 'visitor'" />
        <ManagementView v-if="userRole === 'admin' && activeMenu === 'management'" />
        <AnalysisView v-if="userRole === 'admin' && activeMenu === 'analysis'" />
      </el-main>
    </el-container>
  </el-container>
</template>

<style scoped>
.layout-container {
  height: 100%;
  width: 100%;
}

.header {
  background-color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 60px;
}

.logo-container {
  display: flex;
  align-items: center;
}

.toggle-icon {
  font-size: 20px;
  margin-right: 15px;
  cursor: pointer;
}

.logo-text {
  margin: 0;
  font-size: 18px;
  color: #333;
}

.header-right {
  display: flex;
  align-items: center;
}

.sidebar-menu {
  height: 100%;
  border-right: none;
}

.sidebar-menu:not(.el-menu--collapse) {
  width: 200px;
}

.main-content {
  padding: 0;
  height: calc(100vh - 60px);
  overflow: auto;  /* 从 hidden 改为 auto，允许滚动 */
}
</style>
